package com.waitingresult.demo.entity;

//import com.fasterxml.jackson.annotation.JsonInclude;
import com.waitingresult.demo.enums.ErrorMessageEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 *@Author:  WangShuai
 *@CreateTime: 2022-10-19  23:25
 *@Description:
 *@Version: 1.0
*/
@Data
@NoArgsConstructor
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    //@JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;
    //@JsonInclude(JsonInclude.Include.NON_NULL)
    private Integer code;
    // @JsonInclude(JsonInclude.Include.NON_NULL)
    private String msg;
    // @JsonInclude(JsonInclude.Include.NON_NULL)
    private String token;

    public Result(T data) {
        this.data = data;
    }

    public Result(String msg, T data) {
        this.msg = msg;
        this.data = data;
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(Integer code, String msg, T data, String token) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.token = token;
    }

    /**
     * 成功的构造
     */
    public static <T> Result<T> success(String msg) {
        return new Result<T>(200, msg, null);
    }
    public static <T> Result<T> success(T data) {
        return new Result<T>(200, "ok", data);
    }
    public static <T> Result<T> success() {
        return new Result<T>(200, "ok", null);
    }
    public static <T> Result<T> success(String msg, T data) {
        return new Result<T>(200, msg, data);
    }

    public static <T> Result<T> success(String msg, T data, String token) {
        return new Result<T>(200, msg, data, token);
    }

    /**
     * 失败的构造
     */

    public static <T> Result<T> error(ErrorMessageEnum messageEnum) {
        return new Result<T>(messageEnum.getCode(), messageEnum.getMsg(), null);
    }

    public static <T> Result<T> error(ErrorMessageEnum messageEnum, T data) {
        return new Result<T>(messageEnum.getCode(), messageEnum.getMsg(), data);
    }

    public static <T> Result<T> error(String msg) {
        return new Result<T>(500, msg, null);
    }

    public static <T> Result<T> error(String msg, T data) {
        return new Result<T>(500, msg, data);
    }

    public static <T> Result<T> error(String msg, T data, String token) {
        return new Result<T>(500, msg, data, token);
    }


    @Override
    public String toString() {
        return "RestResult{" + "code=" + code + ", msg='" + msg + '\'' + ", data=" + data + '}';
    }

}